package cn.net.withub.dataCollector.web.controller;

import cn.net.withub.dataCollector.common.base.BaseController;
import cn.net.withub.dataCollector.common.model.TCollectorConfig;
import cn.net.withub.dataCollector.common.model.TCollectorContent;
import cn.net.withub.dataCollector.common.model.TCollectorData;
import cn.net.withub.dataCollector.common.model.TCollectorElement;
import cn.net.withub.dataCollector.common.utils.CreateNewKey;
import cn.net.withub.dataCollector.common.utils.HTMLSpirit;
import cn.net.withub.dataCollector.web.collector.CollectorDocUtils;
import cn.net.withub.dataCollector.web.collector.CollectorReflect;
import cn.net.withub.dataCollector.web.collector.CollectorUtils;
import cn.net.withub.dataCollector.web.service.CollectorService;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by yuanjie on 2018/4/25.
 */
@RequestMapping("/collectorController")
@Controller
@Scope("prototype")
public class CollectorController extends BaseController{

    @Resource
    private CollectorService collectorService;

    @Resource
    private CreateNewKey createNewKey;

    private Logger log = Logger.getLogger(this.getClass());


    @RequestMapping("/test")
    public String test() throws Exception {

        return "/view/test";
    }

    /**
     * 爬取新闻
     * @throws Exception
     */
    @RequestMapping("/collectorData")
    public void collectorData() throws Exception {
        // 得到上下文环境
        WebApplicationContext webContext = ContextLoader.getCurrentWebApplicationContext();

        CollectorUtils crawler = new CollectorUtils("crawl", true,webContext,null);
        crawler.setThreads(50);
        crawler.setTopN(100);
        //crawler.setResumable(true);
        crawler.start(4);

        responseJsonStringData("爬取新闻数据成功!");
    }

    @RequestMapping("/ybTest")
    @ResponseBody
    public Map<String, Object> ybTest() throws Exception {

        Thread.sleep(100);
        Map map = new HashMap();
        map.put("test", "5151515551");
        return map;
    }

    /**
     * 爬取doc文件
     * @throws Exception
     */
    @RequestMapping("/collectorDoc")
    public void collectorDoc() throws Exception {
        // 得到上下文环境
        WebApplicationContext webContext = ContextLoader.getCurrentWebApplicationContext();

        CollectorDocUtils crawler = new CollectorDocUtils("crawl1", true,webContext);
        crawler.setThreads(4);
        crawler.setTopN(100);
        //crawler.setResumable(true);
        //crawler.addSeed("http://149.0.160.10/news/newsList1.shtml;jsessionid=E89FEAEE44B202700CB536E27C71CCA6?page=1&pageSize=5&categoryid=186");
        crawler.start(2);


        responseJsonStringData("爬取doc数据成功!");
    }

    /**
     * 爬取doc文件
     * @throws Exception
     */
    @RequestMapping("/view")
    public String view(String id, ModelAndView modelAndView) throws Exception {
        TCollectorData tCollectorData = null;
        //TCollectorContent tCollectorContent = null;
        String url = "";
        try{
            if(!id.equals("")){
                tCollectorData = (TCollectorData)collectorService.load(TCollectorData.class," id = '"+id+"'");
                /*if(tCollectorData !=null){
                    tCollectorContent = (TCollectorContent)collectorService.load(TCollectorContent.class," data_id = '"+tCollectorData.getId()+"'");
                }*/
            }

            if(tCollectorData!=null){
                if(tCollectorData.getWjgs().equals("pdf")){
                    url = "/view/showPdf";
                    request.setAttribute("wjlj",tCollectorData.getWjlj());
                    request.setAttribute("tCollectorData",tCollectorData);
                }else if(tCollectorData.getWjgs().equals("doc")){
                    url = "/view/download";
                    request.setAttribute("wjlj",tCollectorData.getWjlj());
                    request.setAttribute("title",tCollectorData.getTitle()+"."+tCollectorData.getWjgs());
                }else if(tCollectorData.getWjgs().equals("html")){
                    url = "/view/view";
                    request.setAttribute("tCollectorData",tCollectorData);
                    request.setAttribute("id",id);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return url;
    }

    /**
     * 爬取doc文件
     * @throws Exception
     */
    @RequestMapping("/iframe")
    public String iframe(String id) throws Exception {
        TCollectorContent tCollectorContent = null;
        try{
            if(!id.equals("")){
                tCollectorContent = (TCollectorContent)collectorService.load(TCollectorContent.class," data_id = '"+id+"'");
            }

            request.setAttribute("tCollectorContent",tCollectorContent);
            request.setAttribute("id",id);
        }catch (Exception e){
            e.printStackTrace();
        }

        return "/view/iframe";
    }

    /**
     * 爬取doc文件
     * @throws Exception
     */
    @RequestMapping("/getElementList")
    @ResponseBody
    public List getElementList(String id) throws Exception {
        List<TCollectorElement> tCollectorElement = null;
        try{
            if(!id.equals("")){
                tCollectorElement = (List<TCollectorElement>)collectorService.findList(TCollectorElement.class," data_id = '"+id+"'");
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return tCollectorElement;
    }


    /**
     * 爬取doc文件
     * @throws Exception
     */
    @RequestMapping("/executeCollector")
    public void executeCollector() throws Exception {
        try{
            collectorService.executeCollector();
            log.info("抓取成功.........!");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 爬取doc文件
     * @throws Exception
     */
    @RequestMapping("/extractText")
    @ResponseBody
    public void extractText() throws Exception {
        List<TCollectorData> dataList = null;
        try{
            dataList = (List<TCollectorData>)collectorService.findList(TCollectorData.class," wjgs = 'html'");
            if(dataList !=null && dataList.size()>0){
                for(TCollectorData data : dataList){
                    collectorService.extractText(data);
                }
            }
            //log.info("提取文本成功.........!");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
